package com.openx.cloud.server.autoconfigure.interceptor;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 日志写入请求ID
 *
 * @author yadu
 */
@Slf4j
public class TraceInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String traceId = request.getHeader(FeignRequestInterceptor.X_REQUEST_ID);
        if (StrUtil.isNotEmpty(traceId)) {
            MDC.put(FeignRequestInterceptor.TRACE_ID, traceId);
        }
        return true;
    }
}
